/*
	//// Đây là script của nhóm K.H.A (Khánh: Lý Duy Khánh, Hiếu: Đoàn Đình Hiếu , Anh: Phạm Trọng Anh)
	Nếu xài ở xài cho web không phải do nhóm K.H.A làm, làm ơn ghi rõ nguồn K.H.A
*/

/*Dạng của kha_tab
	bao
	|_tab
	|  |_tab1
	|  |_tab2
	|_tab_content
	  |_noidung1
	  |_noidung2
	Dùng được cho tất cả ul li hoặc div... miễn theo cấu trức trên. events dùng cho click hoặc mouseenter. active là class khi tab1 được click
*/
function kha_tab(tab,tab_content,events,active)
{
	if(!events) events='click';
	if(!active) active='tab_active';
	$(tab).children().each(function (){
	first=$(this).attr('first');
	if(typeof first!='undefined')
	{
		$(this).addClass(active);
		attrtab=$(this).attr('tab');
		$(tab_content).children().each(function (){
		if($(this).attr('tab_content')==attrtab) $(this).css("display","block");
		else $(this).css("display","none");
		});
	}
	});
	$(tab).children().delegate($(tab).children(),events,function (){
		attrtab=$(this).attr('tab');
		if(typeof attrtab!='undefined')
		{
			$(tab).children().removeClass(active)
			$(this).addClass(active);
			$(tab_content).children().each(function (){
				if($(this).attr('tab_content')==attrtab) $(this).css("display","block");
				else $(this).css("display","none");
			});
		}
	});
}

// Dùng để kiểm tra hợp lệ ngày tháng không. Theo định dạng dd/mm/yyyy kết quả trả về là true or false
function kha_checkdate(ngay,thang,nam){
		
		mm1 = parseInt(thang,10); 
		dd1 = parseInt(ngay,10);
		yy1 = parseInt(nam);//alert(dd1);
		if(isNaN(dd1) || isNaN(mm1) || isNaN(yy1)){
			return false;
		}
		dt2 = new Date(mm1+'/'+dd1+'/'+yy1)
		dd2 = dt2.getDate();
		mm2 = dt2.getMonth()+1;
		yy2 = dt2.getFullYear();
		if(dd1==dd2 && mm1==mm2 && yy1==yy2)
			{
			return true;}
		else
			{
			return false;}
}

/**
 * @author hieu
 */
var dashboard = {
};

dashboard.is_arr = function(arr) { return (arr != null && arr.constructor == Array) };

dashboard.is_str = function(str) { return (str && (/string/).test(typeof str)) };

dashboard.is_func = function(func) { return (func != null && func.constructor == Function) };

dashboard.is_num = function(num) { var num = Number(num); return (num != null && !isNaN(num)) };

dashboard.is_int = function (x) {var y=parseInt(x);if (isNaN(y)) return false;return x==y && x.toString()==y.toString();} 

dashboard.is_obj = function(obj) { return (obj != null && obj instanceof Object) };

dashboard.is_exists = function(obj) { return (obj != null && obj != undefined && obj != "undefined") };

dashboard.is_blank = function(str) { return (dashboard.util_trim(str) == "") };

dashboard.is_email = function(str) {return (/^[a-z-_0-9\.]+@[a-z-_=>0-9\.]+\.[a-z]{2,3}$/i).test(dashboard.util_trim(str))};

dashboard.is_username = function(value){ return (value.match(/^[0-9]/) == null) && (value.search(/^[0-9_a-zA-Z]*$/) > -1); }

dashboard.is_link = function(str){ return (/(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/).test(dashboard.util_trim(str)) };

dashboard.is_image = function(imagePath){
  var fileType = imagePath.substring(imagePath.lastIndexOf("."),imagePath.length).toLowerCase();
  return (fileType == ".gif") || (fileType == ".jpg") || (fileType == ".png") || (fileType == ".jpeg");
};

dashboard.is_ff  = function(){ return (/Firefox/).test(navigator.userAgent) };

dashboard.is_ie  = function() { return (/MSIE/).test(navigator.userAgent) };

dashboard.is_ie6 = function() { return (/MSIE 6/).test(navigator.userAgent) };

dashboard.is_ie7 = function() { return (/MSIE 7/).test(navigator.userAgent) };

dashboard.is_ie8 = function() { return (/MSIE 8/).test(navigator.userAgent) };

dashboard.is_chrome = function(){ return (/Chrome/).test(navigator.userAgent) };

dashboard.is_opera = function() { return (/Opera/).test(navigator.userAgent) };

dashboard.is_safari = function(){ return (/Safari/).test(navigator.userAgent) };

dashboard.is_ele = function(ele) { return (ele && ele.tagName && ele.nodeType == 1) };

dashboard.redirect = function(url){window.location = url};
dashboard.join = function(str) {
	var store = [str];
	return function extend(other) {
		if (other != null && 'string' == typeof other){
			store.push(other);
			return extend;
		}	
		return store.join('');
	}
};
/**
 * Utils
 */
dashboard.trim = function(str) {return (/string/).test(typeof str) ? str.replace(/^\s+|\s+$/g, "") : ""};

dashboard.util_random = function(a, b) { return Math.floor(Math.random() * (b - a + 1)) + a };

dashboard.get_ele = function(id) { return document.getElementById(id) };
dashboard.valid_code = function(txt){
	var invalidCode = "`~!@#$%^&*()-_=+\\|[{]};:'\",<.>/?";
	for (var i = 0; i < invalidCode.length; i++) {
	    for (var j = 0; j < txt.length; j++) {
	        if (txt.charAt(j) == invalidCode.charAt(i)) {
	            return false;
	        }
	    }
	}
	return true;
}
dashboard.valid_code_lite = function(txt){
	var invalidCode = "'\"";
	for (var i = 0; i < invalidCode.length; i++) {
	    for (var j = 0; j < txt.length; j++) {
	        if (txt.charAt(j) == invalidCode.charAt(i)) {
	            return false;
	        }
	    }
	}
	return true;
}

dashboard.valid_codefor_username = function (txt) {
	var invalidCode = "`~!@#$%^&*()-_=+\\|[{]};:\",<.>/?";
	for (var i = 0; i < invalidCode.length; i++) {
	    for (var j = 0; j < txt.length; j++) {
	        if (txt.charAt(j) == invalidCode.charAt(i)) {
	            return false;
	        }
	    }
	}
	return true;
}
dashboard.contains_number = function (txt) {
	var seriesNumber = "0123456789";
	for (var i = 0; i < seriesNumber.length; i++) {
	    for (var j = 0; j < txt.length; j++) {
	        if (txt.charAt(j) == seriesNumber.charAt(i)) {
	            return true;
	        }
	    }
	}
	return false;
}
dashboard.validate_email = function(email) {
	// var filter = /^[a-zA-Z](\.?([a-zA-Z0-9_-]+)*)([?!\.])+\@([a-zA-Z0-9\-_]+\.)+[a-zA-Z]{2,4}$/;	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return filter.test(email);	
}
dashboard.cookie = {
	clearAllCookies : function () {
		var cookies = document.cookie.split(";");
		var ck = "";
		for(var i = 0; i < cookies.length; i++) {
			ck = jQuery.trim(cookies[i]).split("=")[0]; //get cookie name
		    if ((ck.substr(0, 3) == config_variable.cookie.prefix) && (ck != config_variable.cookie.remember_me) && (ck != config_variable.cookie.username_login)) {
		        jQuery.cookie(ck, null, { path: '/' });
		    }
		}
	},
}

dashboard.client_include = function(id, url){
	  var req = false;
	  // For Safari, Firefox, and other non-MS browsers
	  if (window.XMLHttpRequest) {
	    try {
	      req = new XMLHttpRequest();
	    } catch (e) {
	      req = false;
	    }
	  } else if (window.ActiveXObject) {
	    // For Internet Explorer on Windows
	    try {
	      req = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try {
	        req = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e) {
	        req = false;
	      }
	    }
	  }
	 var element = document.getElementById(id);
	 if (!element) {
	  alert("Bad id " + id +
	   "passed to clientSideInclude." +
	   "You need a div or span element " +
	   "with this id in your page.");
	  return;
	 }
	  if (req) {
	    // Synchronous request, wait till we have it all
	    req.open('GET', url, false);
	    req.send(null);
	    element.innerHTML = req.responseText;
	  } else {
	    element.innerHTML =
	   "Sorry, your browser does not support " +
	      "XMLHTTPRequest objects. This page requires " +
	      "Internet Explorer 5 or better for Windows, " +
	      "or Firefox for any system, or Safari. Other " +
	      "compatible browsers may also exist.";
	  }
}

dashboard.generate_path = function(arrParams){
	var path = domainWS;
	for (var i = 0; i < arrParams.length; i++) {
		if (i < (arrParams.length - 1)) {
			path += arrParams[i] + "/";
		} else {
			path += arrParams[i] + "?rnd=" + Math.random();
		}
	}
	return path;
}
dashboard.get_url_parameter = function(name) {
    return decodeURIComponent(
        (location.search.match(RegExp("[?|&]"+name+'=(.+?)(&|$)'))||[,null])[1]
    );  
}
dashboard.util_date = {
	isLeapYear : function (yr) {
	    //Every year divisible by 4 is a leap year.
	    //But every year divisible by 100 is NOT a leap year
	    //Unless the year is also divisible by 400, then it is still a leap year.
	
		if ((parseInt(yr) % 4) == 0) {
	  		if (parseInt(yr) % 100 == 0) {
	    		if (parseInt(yr) % 400 != 0) {
	    			//Not Leap
	    			return false;
	    		} else {
		    		//Leap
		    		return true;
		    	}
	  		} else {
		    	//Leap
		    	return true;
		  	}
	 	} else {
	    	//Not Leap
	    	return false;
	 	}
	},
	/**
	 * Check string is date mm/dd/yyyy
	 *  
	 *  Checks for dd/mm/yyyy format.
	 *  dtDay = dtArray[1];
	 *  dtMonth= dtArray[3];
	 *  dtYear = dtArray[5];
	 *    
	 *  Check for mm/dd/yyyy
	 *  dtMonth = dtArray[1];
	 *  dtDay= dtArray[3];
	 *  dtYear = dtArray[5]; 
	 * 
	 * if(isDate(txtDate)){
	 * 	alert('valid Date)
	 * }else{
	 * 	alert('Invalid Date)
	 * }
 	 * @param {Object} txtDate
	 */
	is_date : function (txtDate)
	{
	  var currVal = txtDate;
	  if(currVal == '')
	    return false;
	  
	  //Declare Regex  
	  var rxDatePattern = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/; 
	  var dtArray = currVal.match(rxDatePattern); // is format OK?
	
	  if (dtArray == null)
	     return false;
	 
	  //Checks for mm/dd/yyyy format.
	  dtMonth = dtArray[1];
	  dtDay= dtArray[3];
	  dtYear = dtArray[5];
	
	  if (dtMonth < 1 || dtMonth > 12)
	      return false;
	  else if (dtDay < 1 || dtDay> 31)
	      return false;
	  else if ((dtMonth==4 || dtMonth==6 || dtMonth==9 || dtMonth==11) && dtDay ==31)
	      return false;
	  else if (dtMonth == 2)
	  {
	     var isleap = (dtYear % 4 == 0 && (dtYear % 100 != 0 || dtYear % 400 == 0));
	     if (dtDay> 29 || (dtDay ==29 && !isleap))
	          return false;
	  }
	  return true;
	},
	/**
	 * Check valid Time hh:mm
 	 * @param {Object} txtTime
 	 * @return boolean
	 */
	is_time : function(txtTime){
		var currVal = txtTime;
		if(currVal == '')
			return false;
		var rxTimePattern = /^(\d{1,2})(:)(\d{1,2})$/; 
		var timeArray = currVal.match(rxTimePattern);
		
		if(timeArray == null)
			return false;
		
		tHours = timeArray[1];
		tMinues = timeArray[3];
		
		if(tHours < 0 || tHours >= 12)
			return false;
		if(tMinues < 0 || tMinues >= 60)
			return false		
		return true;
	}

}
	  
dashboard.get_url_vars = function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
}
dashboard.get_url_var = function(name){
    return dashboard.get_url_vars()[name];
} 
